/*
 * Credits goes to http://www.apriorit.com/our-company/dev-blog/227-handle-sms-on-android
 */
package emt.friendtracking.smsmodule;

import android.util.Log;
import emt.friendtracking.DecryptionException;
import emt.friendtracking.maps.AnswerTracking.Type;

public class Message
{
	private static final String TAG = Message.class.getSimpleName();
	/**
	 * Encryption password for the SMS sending and receiving service. It is an array with the length of 7.
	 */
	public static final byte[] PASSWORD = new byte[]{ 0x20, 0x32, 0x34, 0x47, (byte) 0x84, 0x33, 0x58 };
	
	private enum MessageType 
		{
			ANSWER_ACCEPT, ANSWER_DECLINE, FIND_ME, ANSWER_SPEED;
			
			private static MessageType get(String s)
			{
				if(s.equalsIgnoreCase(ANSWER_ACCEPT.name()))
					return ANSWER_ACCEPT;
				else if(s.equalsIgnoreCase(ANSWER_DECLINE.name()))
					return ANSWER_DECLINE;
				else if(s.equalsIgnoreCase(FIND_ME.name()))
					return FIND_ME;
				else if(s.equalsIgnoreCase(ANSWER_SPEED.name()))
					return MessageType.ANSWER_SPEED;
				else
					return null;
			}
		};
	private static final String seperator = ":";
	
	public String username;
	public Type type;
	public String distance;
	public int timeToArrival;
	public double longitude;
	public double latitude;
	
	public Message(String username, Type type, String distance)
	{
		this.username = username;
		this.type = type;
		this.distance = distance;
	}

	public Message(String username, Type type)
	{
		this.username = username;
		this.type = type;
	}

	public Message(String username, Type type, int minutes)
	{
		this.username = username;
		this.type = type;
		this.timeToArrival = minutes;
	}

	public Message(String username, double longitude, double latitude)
	{
		this.username = username;
		this.type = null;
		this.longitude = longitude;
		this.latitude = latitude;
	}

	public static String make(String username, Type answer, String distance) throws Exception
	{
		String message = "";
		switch(answer)
		{
			case ACCEPT:
				message = MessageType.ANSWER_ACCEPT.name() + seperator;
				message = message + username + seperator;
				message = message + distance;
				break;
			case DECLINE:
				message = MessageType.ANSWER_DECLINE.name() + seperator;
				message = message + username;
				break;
		}

		Log.i(TAG, "Coordinate to sent:" + message); //TODO "Coordinate to sent: XXXX:XXXX.X:XXXX.X"
		return StringCryptor.encrypt(new String(PASSWORD), message);
	}

	public static String make(String username, int minutes) throws Exception
	{
		String message = MessageType.ANSWER_SPEED.name() + seperator;
		message = message + username + seperator;
		message = message + Integer.toString(minutes);

		Log.i(TAG, "Coordinate to sent:" + message); //TODO "Coordinate to sent: XXXX:XXXX.X:XXXX.X"
		return StringCryptor.encrypt(new String(PASSWORD), message);
	}

	public static String make(String username, double longitude, double latitude) throws Exception
	{
		String message = MessageType.FIND_ME.name() + seperator;
		message = message + username + seperator;
		message = message + Double.toString(longitude) + seperator;
		message = message + Double.toString(latitude);

		Log.i(TAG, "Coordinate to sent:" + message); //TODO "Coordinate to sent: XXXX:XXXX.X:XXXX.X"
		return StringCryptor.encrypt(new String(PASSWORD), message);
	}
	
	
	public static Message get(String message) throws DecryptionException
	{
		String msg = null;
		try
		{
			msg = StringCryptor.decrypt(new String(PASSWORD), message);
			Log.d(TAG, "SMS text: " + msg);

		}
		catch(Exception e)
		{
			throw new DecryptionException();
		}
		
		String information[] = msg.split(seperator);
		
		MessageType type = MessageType.get(information[0]);
		Message Msg = null;
		
		switch(type)
		{
			case ANSWER_ACCEPT:
				Msg = new Message(information[1], Type.ACCEPT, information[2]);
				break;
			case ANSWER_DECLINE:
				Msg = new Message(information[1], Type.DECLINE);
				break;
			case ANSWER_SPEED:
				Msg = new Message(information[1], Type.TIME, Integer.parseInt(information[2]));
				break;
			case FIND_ME:
				Msg = new Message(information[1], Double.parseDouble(information[2]), Double.parseDouble(information[3]));
				break;
			default:
		}
		
		
		return Msg;
	}
	
	
		
}

